
-- ***************************************************************************************************
-- SELECT INCREMENTAL DELETE Stored Procedures for the list of tables mentioned in the Initialize file
------------------------------------------------------------------------------------------------------

--TABLE NAME: Vendor    STORED PROCEDURE NAME: Sales.sync_sp_Vendor_SelectIncrementalDeletes

use [SyncSamplesDb_ChangeTracking_Custom]

if object_id(N'Sales.sync_sp_Vendor_SelectIncrementalDeletes', 'P') is not null
	drop procedure Sales.sync_sp_Vendor_SelectIncrementalDeletes
GO	

	create procedure Sales.sync_sp_Vendor_SelectIncrementalDeletes (	
		@sync_last_received_anchor bigint, 
		@sync_new_received_anchor bigint, 
		@sync_initialized bit,
		@sync_client_id_binary varbinary)   
AS
	IF @sync_initialized > 0  
	BEGIN 
		SELECT		CT.VendorId
		FROM		CHANGETABLE(CHANGES [SyncSamplesDb_ChangeTracking_Custom].[Sales].[Vendor], @sync_last_received_anchor) AS CT
		WHERE		CT.SYS_CHANGE_VERSION <= @sync_new_received_anchor
					AND CT.SYS_CHANGE_OPERATION = 'D'
					AND (CT.SYS_CHANGE_CONTEXT IS NULL 
						OR CT.SYS_CHANGE_CONTEXT <> @sync_client_id_binary) 

		IF (CHANGE_TRACKING_MIN_VALID_VERSION(OBJECT_ID('[SyncSamplesDb_ChangeTracking_Custom].[Sales].[Vendor]')) > @sync_last_received_anchor) 
			RAISERROR (N'SQL Server Change Tracking has cleaned up tracking information for table ''%s''. 
					To recover from this error, the client must reinitialize its local database and try again'
					,16,3,'[SyncSamplesDb_ChangeTracking_Custom].[Sales].[Vendor]')  
	END

GO
-------------------------------------------END------------------------------------------------------------------
